const reg = {
  number: "^[-]?\\d+(\\.\\d+)?$",
  letter: "^[A-Za-z]+$",
  letterAndNumber: "^[A-Za-z0-9]+$",
  phone: "\\d{6,20}",
  mobilePhone: "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$",
  fixPhone: "^([0-9]{3,4}-)?[0-9]{7,8}$",
  mobileAndFixPhone:
    "((^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$)|(^([0-9]{3,4}-)?[0-9]{7,8}$))",
  validCode: "^[0-9]{6}$",
  password: "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z+&@#/%?=~_|!:,.;]{6,20}$",
  letterStartNumberIncluded: "^[A-Za-z]+[A-Za-z\\d]*$",
  noChinese: "^[^\u4e00-\u9fa5]+$",
  chinese: "^[\u4e00-\u9fa5]+$",
  email: "^([-_A-Za-z0-9.]+)@([_A-Za-z0-9]+\\.)+[A-Za-z0-9]{2,3}$",
  url: "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]",

  // 非零正整数
  nonZeroPositiveInteger: "^(?!0)\\d+$",
};

/**
 * 判断是否为空
 * @param val 数据
 */
export const isNull = (val: any) => {
  if (typeof val === "boolean") {
    return false;
  }
  if (typeof val === "number") {
    return false;
  }
  if (val instanceof Array) {
    if (val.length === 0) return true;
  } else if (val instanceof Object) {
    if (JSON.stringify(val) === "{}") return true;
  } else {
    if (
      val === "null" ||
      val == null ||
      val === "undefined" ||
      val === undefined ||
      val === ""
    )
      return true;
    return false;
  }
  return false;
};

/**
 * 邮箱
 * @param {*} s
 */
export function isEmail(s: string) {
  return new RegExp(reg.email).test(s);
}

/**
 * 手机号码
 * @param {*} s
 */
export function isMobile(s: string) {
  return new RegExp(reg.mobilePhone).test(s);
}

/**
 * 手机号码
 * @param {*} s
 */
export function isPhone(s: string) {
  return new RegExp(reg.phone).test(s);
}

/**
 * 电话号码
 * @param {*} s
 */
export function isFixPhone(s: string) {
  return new RegExp(reg.fixPhone).test(s);
}

/**
 * 验证码
 * @param {*} s
 */
export function isValidCode(s: string) {
  return new RegExp(reg.validCode).test(s);
}

/**
 * 密码
 * @param {*} s
 */
export function isPassword(s: string) {
  return new RegExp(reg.password).test(s);
}

/**
 * URL地址
 * @param {*} s
 */
export function isURL(s: string) {
  return new RegExp(reg.url).test(s);
}

/* 小写字母 */
export function isLowerCase(str: string) {
  const reg = /^[a-z]+$/;
  return reg.test(str);
}

/* 大写字母 */
export function isUpperCase(str: string) {
  const reg = /^[A-Z]+$/;
  return reg.test(str);
}

/* 大小写字母 */
export function isAlphabets(str: string) {
  const reg = /^[A-Za-z]+$/;
  return reg.test(str);
}

/* 验证pad还是pc */
export const isPc = function () {
  const userAgentInfo = navigator.userAgent;
  const Agents = [
    "Android",
    "iPhone",
    "SymbianOS",
    "Windows Phone",
    "iPad",
    "iPod",
  ];
  let flag = true;
  for (var v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > 0) {
      flag = false;
      break;
    }
  }
  return flag;
};

/**
 * 判断是否为整数
 */
export function isNum(num: string, type: number) {
  let regName = /[^\d.]/g;
  if (type == 1) {
    if (!regName.test(num)) return false;
  } else if (type == 2) {
    regName = /[^\d]/g;
    if (!regName.test(num)) return false;
  }
  return true;
}

/**
 * 判断是否为小数
 */
export function isNumWord(num: string, type: number) {
  let regName = /[^\d.]/g;
  if (type == 1) {
    if (!regName.test(num)) return false;
  } else if (type == 2) {
    regName = /[^\d.]/g;
    if (!regName.test(num)) return false;
  }
  return true;
}

export default {
  isNull,
  isEmail,
  isMobile,
  isPhone,
  isURL,
  isLowerCase,
  isUpperCase,
  isAlphabets,
  isPc,
  isNum,
  isNumWord,
};
